<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/WEB-INF/webpage/common/taglibs.jspf" %>
<!DOCTYPE html>
<html>
<head>
    <title>首页管理列表</title>
    <meta name="decorator" content="list"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <html:css name="iCheck,Validform,jquery-ztree,easy-ui"/>
    <html:js name="iCheck,Validform,jquery-ztree,easy-ui,public-js"/>
    <style type="text/css">.row {
        margin: 0;
    }</style>
    <script type="text/javascript" src="${staticPath}/common/js/curdtools_jqgrid_extend.js"></script>
</head>
<body title="首页管理">
<div class="easyui-layout" fit="true" id="cc" style="width:100%;">
    <div data-options="region:'center'" title="首页管理">
        <grid:grid id="sysHomepageManageGridId" url="${adminPath}/sys/syshomepagemanage/ajaxList"
                   dbUrl="${adminPath}/sys/syshomepagemanage/{id}">
            <grid:column label="sys.common.key" hidden="true" name="id" width="100"/>
            <grid:column label="sys.common.opt" name="opt" formatter="button" width="100"/>
            <shiro:hasPermission name="sys:syshomepagemanage:binduser">
                <grid:button title="关联用户" groupname="opt" function="toBindUser" outclass="btn-warning" winwidth="300px"
                             innerclass="fa-key"/>
                <grid:button title="取消关联" groupname="opt" function="toCancelUser" outclass="btn-primary"
                             winwidth="300px"
                             innerclass="fa-unlock"/>
            </shiro:hasPermission>
            <grid:column label="首页名称" name="homePageName"/>
            <grid:column label="首页链接路径" name="homePageLink"/>
            <grid:column label="文件路径" name="filePath"/>
            <grid:column label="文件名称" name="fileName"/>
            <grid:column label="是否默认" name="isDefault" dict="sf"/>
            <%-- <grid:column label="是否启用" dict="sf" name="status"/>--%>
            <grid:column label="备注" name="remarks"/>
            <shiro:hasPermission name="sys:syshomepagemanage:create">
                <grid:toolbar function="create"/>
            </shiro:hasPermission>
            <shiro:hasPermission name="sys:syshomepagemanage:update">
                <grid:toolbar function="update"/>
            </shiro:hasPermission>
            <shiro:hasPermission name="sys:syshomepagemanage:delete">
                <grid:toolbar function="delete"/>
            </shiro:hasPermission>
            <shiro:hasPermission name="sys:syshomepagemanage:isdefault">
                <grid:toolbar title="设置默认" onclick="toSetDefault()"/>
            </shiro:hasPermission>
            <grid:toolbar function="search"/>
            <grid:toolbar function="reset"/>
        </grid:grid>
    </div>
</div>
<script>

    var gridId = "sysHomepageManageGridIdGrid";
    /*关联用户*/
    function toBindUser(title, url, gridId, id, width, height, callback) {
        if (url == undefined || url == null || url == "") {
            url = "${adminPath}/sys/syshomepagemanage/toBindUser";
        }
        top.layer.open({
            type: 2,
            area: ["90%", "80%"],
            title: title,
            maxmin: true, //开启最大化最小化按钮
            content: url,
            btn: ['确定', '关闭'],
            yes: function (index, layero) {
                var body = top.layer.getChildFrame('body', index);
                var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                //文档地址
                //http://www.layui.com/doc/modules/layer.html#use
                var userIds = iframeWin.contentWindow.getUserIds();
                if (userIds.length == 0) {
                    return;
                }
                var savePath = "${adminPath}/sys/syshomepagemanage/toSaveBind?homePageId=" + id;
                var ids = [];
                //确定
                for (var i = 0; i < userIds.length; i++) {
                    ids.push(userIds[i]);
                }
                $.ajax({
                    url: savePath,
                    type: 'post',
                    data: {
                        ids: ids.join(',')
                    },
                    cache: false,
                    success: function (d) {
                        top.layer.close(index);
                        if (d.ret == 0) {
                            var msg = d.msg;
                            layer.msg(msg, {icon: 1});

                        } else {
                            var msg = d.msg;
                            layer.msg(msg, {icon: 3, shade: 0.3, time: 1000});
                        }
                    }
                });
            },
            cancel: function (index) {
            }
        });
    }

    /*取消关联*/
    function toCancelUser(title, url, gridId, id, width, height, callback) {
        if (url == undefined || url == null || url == "") {
            url = "${adminPath}/sys/syshomepagemanage/toCancelUser?homePageId=" + id;
        }
        top.layer.open({
            type: 2,
            area: ["90%", "80%"],
            title: title,
            maxmin: true, //开启最大化最小化按钮
            content: url,
            btn: ['确定', '关闭'],
            yes: function (index, layero) {
                var body = top.layer.getChildFrame('body', index);
                var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                //文档地址
                //http://www.layui.com/doc/modules/layer.html#use
                var userIds = iframeWin.contentWindow.getUserIds();
                if (userIds.length == 0) {
                    return;
                }
                var savePath = "${adminPath}/sys/syshomepagemanage/toSaveCancel?homePageId=" + id;
                var ids = [];
                //确定
                for (var i = 0; i < userIds.length; i++) {
                    ids.push(userIds[i]);
                }
                $.ajax({
                    url: savePath,
                    type: 'post',
                    data: {
                        ids: ids.join(',')
                    },
                    cache: false,
                    success: function (d) {
                        top.layer.close(index);
                        if (d.ret == 0) {
                            var msg = d.msg;
                            layer.msg(msg, {icon: 1});

                        } else {
                            var msg = d.msg;
                            layer.msg(msg, {icon: 3, shade: 0.3, time: 1000});
                        }
                    }
                });
            },
            cancel: function (index) {
            }
        });
    }

    /*设置默认*/
    function toSetDefault() {
        var url ="${adminPath}/sys/syshomepagemanage/toSetDefault"
        var rows = $("#" + gridId).jqGrid('getGridParam', 'selarrrow');
        var rowData = $("#" + gridId).jqGrid('getGridParam', 'selrow');
        var multiselect = $("#" + gridId).jqGrid('getGridParam', 'multiselect');
        if (!multiselect) {
            if (rowData) {
                rows[0] = rowData;
            }
        }
        if (rows.length == 1) {
            layer.confirm("您确定要设置选择项为默认首页吗！", {
                title: "提示",
                icon: 3,
                btn: ['确定', '取消'] //按钮
            }, function () {
                $.ajax({
                    url: url,
                    type: 'post',
                    data: {
                        id: rows[0]
                    },
                    cache: false,
                    success: function (d) {
                        if (d.ret == 0) {
                            var msg = d.msg;
                            layer.msg(msg, {icon: 1});
                            //刷新表单
                            refreshTable(gridId);
                        } else {
                            var msg = d.msg;
                            layer.msg(msg, {icon: 3, shade: 0.3, time: 1000});
                        }
                    }
                });
            }, function () {
                //取消
            });
            return;
        } else if (rows.length > 1) {
            top.layer.msg('只能选择一条数据!', {icon: 0, time: 1000});
            return;
        } else {
            top.layer.msg('请选择需要设置的数据!', {icon: 0, time: 1000});
            return;
        }

    }
</script>
</body>
</html>